﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Calculator
{
    public partial class Calculator : System.Web.UI.Page
    {
        enum operations { Add, Substract, Multiply, Divide };

        protected void button_Add(object sender, EventArgs e)
        {
            firstNumber.Text = number.Text;
            number.Text = string.Empty;
            Application["operation"] = operations.Add;
        }

        protected void button_Substract(object sender, EventArgs e)
        {
            firstNumber.Text = number.Text;
            number.Text = string.Empty;
            Application["operation"] = operations.Substract;
        }

        protected void button_Multiply(object sender, EventArgs e)
        {
            firstNumber.Text = number.Text;
            number.Text = string.Empty;
            Application["operation"] = operations.Multiply;
        }

        protected void button_Divide(object sender, EventArgs e)
        {
            firstNumber.Text = number.Text;
            number.Text = string.Empty;
            Application["operation"] = operations.Divide;
        }

        protected void button_Sqrt(object sender, EventArgs e)
        {
            result.Text = Math.Sqrt(double.Parse(number.Text)).ToString();
            firstNumber.Text = number.Text;
            number.Text = string.Empty;
        }

        protected void button_Module(object sender, EventArgs e)
        {
            double calculatedNumber = double.Parse(number.Text);
            firstNumber.Text = number.Text;
            number.Text = string.Empty;
            if (calculatedNumber<0)
            {
                calculatedNumber = -calculatedNumber;
            }
            result.Text = calculatedNumber.ToString();
        }

        protected void button_Equals(object sender, EventArgs e)
        {
            double numberOne = double.Parse(firstNumber.Text);
            double numberTwo = double.Parse(number.Text);
            double resultNumber;
            switch ((operations)Application["operation"])
            {
                case operations.Add:
                    resultNumber = numberOne + numberTwo;
                    break;
                case operations.Substract:
                    resultNumber = numberOne - numberTwo;
                    break;
                case operations.Multiply:
                    resultNumber = numberOne * numberTwo;
                    break;
                case operations.Divide:
                    resultNumber = numberOne / numberTwo;
                    break;
                default:
                    resultNumber = 0;
                    break;
            }
            result.Text = resultNumber.ToString();
        }
    }
}